<?php
namespace Hive\Ressource;
use Hive\Ressource;
/**
 * Interview Object.
 *
 * To store interview minutes and comments, there are different types of interview.
 *
 * @author Xavier VILAIN
 * @package ressource
 * @property int 	$iid 			Interview Id
 * @property int 	$ressource_id	Id of the interviewed ressource
 * @property int 	$interviewer	Id of the interviewer
 * @property string $interview_status	Status
 * @property string $interview_type	Plan Interview Date
 * @property string $text1			Interview Comment 1
 * @property string $text2			Interview Comment 2
 * @property string $text3			Interview Comment 3
 * @property bool 	$loekz 		Deletion Flag if true is an been deleted
 */
class Interview extends \Fox\Object {

	/**
	 * Link to the ressource
	 *
	 * @var string
	 */
	const LINK_RESSOURCE = 'ressource';
	
	/**
	 *
	 * Return Object Content for Search Engine
	 */
	public function content()
	{
		$data = array(
			'text1' => $this->text1,
			'text2' => $this->text2,
			'text3' => $this->text3,
			'interviewer' => $this->interviewer,
			'interview_type' => $this->interview_type);

		if(!empty($data['interview_type'])) {
			$data['interview_type_descr'] = \Fox\Ddic\DataElement\Text::label("interview_type",$data['interview_type']);
		}
		return implode(',',$data);
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::afterVersionCreate()
	 */
	protected function afterCreate()
	{
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->add($this,$this->ressource_id,Ressource::classname());
	}

	protected function beforeUpdate($changed) {
		if($changed) {
			$this->udate = self::sqlDateTime(time());
		}
		return $changed;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::afterVersionUpdate()
	 */
	protected function afterUpdate()
	{
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->ressource_id,Ressource::classname());
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::delete()
	 */
	public function delete() {
		parent::delete();
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->delete($this,$this->ressource_id,Ressource::classname());
	}
	
	/**
	 * For an interview the label is the plan date of the interview
	 * @return string
	 */
	public function label() {
		return Msg(\Hive::BUNDLE,\Hive::MSG_INTERVIEW_OF,
			\Fox\Utilities\Dates::dateTimeToFullString($this->plan_date));
	}
	
	/**
	 * Find and Return Interview Status Object
	 * @return \Hive\Contact\Interview\Status
	 */
	public function status() {
		try {
			return \Fox\Dao::load(\Hive\Contact\Interview\Status::className(),$this->interview_status);
		} catch(\Fox\Object\Exception\NotExist $e) {
			return new \Hive\Contact\Interview\Status();
		}
		
	}
}
\Fox\Dao::infos(
	Interview::className(),
	new \Fox\Object\Info('ressource_interviews', \Fox\Databases::DB_APP)
)->addAutoIncrementField('iid');